package com.tdwyz.fictionmanage.utils;

import java.util.Map;

/**
 * ThreadLocal 工具类
 */
@SuppressWarnings("all")
public class ThreadLocalUtil {
    //提供ThreadLocal对象,
    private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();

    //根据键获取值
    public static <T> T get(){
        return (T) THREAD_LOCAL.get();
    }

    //存储键值对
    public static void set(Object value){
        THREAD_LOCAL.set(value);
    }


    //清除ThreadLocal 防止内存泄漏
    public static void remove(){
        THREAD_LOCAL.remove();
    }

    //获取当前登录用户id
    public static Integer getUserId(){
        Map<String,Object> map = ThreadLocalUtil.get();
        int id = (int)map.get("id");
        return id;
    }

    //获取当前登录用户名
    public static String getUserName(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        return username;
    }
}
